#include "LoginUI.h"
#include <QMouseEvent>
#include <QStyle>

LoginUI::LoginUI(QWidget *parent) : QWidget(parent),
    m_userEdit(nullptr), m_pwdEdit(nullptr), m_confirmPwdEdit(nullptr),
    m_loginBtn(nullptr), m_regBtn(nullptr), m_closeBtn(nullptr),
    m_isDragging(false)
{
    initUI();
    resize(800,600);
}

void LoginUI::initUI()
{
    // 主容器（用于实现边框和阴影效果）
    QWidget *mainContainer = new QWidget(this);
    mainContainer->setStyleSheet(
        "background-color: #1a1a1a; "
        "border-radius: 10px; "
        "box-shadow: 0 0 15px rgba(0, 0, 0, 0.3);"
    );

    // 标题栏（用于拖动窗口）
    QWidget *titleBar = new QWidget(mainContainer);
    titleBar->setStyleSheet("background-color: transparent;");
    titleBar->setFixedHeight(40);

    // 关闭按钮
    m_closeBtn = new QPushButton("×", titleBar);
    m_closeBtn->setStyleSheet(
        "QPushButton { color: #bbbbbb; background-color: transparent; "
        "font-size: 16px; border: none; border-radius: 15px; }"
        "QPushButton:hover { background-color: #e53935; color: white; }"
        "QPushButton:pressed { background-color: #c62828; }"
    );
    m_closeBtn->setFixedSize(30, 30);

    QHBoxLayout *titleLayout = new QHBoxLayout(titleBar);
    titleLayout->addStretch();
    titleLayout->addWidget(m_closeBtn);
    titleLayout->setContentsMargins(0, 5, 5, 0);

    // 窗口标题
    QLabel *titleLabel = new QLabel("天穹监控系统", mainContainer);
    titleLabel->setStyleSheet(
        "color: #ffffff; font-size: 26px; font-weight: bold; "
        "padding: 10px; border-bottom: 1px solid #2d2d2d;"
    );
    titleLabel->setAlignment(Qt::AlignCenter);

    // 账号输入区
    QWidget *userWidget = new QWidget(mainContainer);
    userWidget->setStyleSheet("background-color: transparent;");
    QHBoxLayout *userLayout = new QHBoxLayout(userWidget);

    QLabel *userLabel = new QLabel("账号:", userWidget);
    userLabel->setStyleSheet("color: #bbbbbb; font-size: 18px; min-width: 60px;");

    m_userEdit = new QLineEdit(userWidget);
    m_userEdit->setPlaceholderText("请输入账号");
    m_userEdit->setStyleSheet(
        "background-color: #2d2d2d; color: white; padding: 10px; "
        "border-radius: 6px; border: 1px solid #3d3d3d; "
        "font-size: 16px; margin: 5px 0px;"
    );
    m_userEdit->setMinimumHeight(40);

    userLayout->addWidget(userLabel);
    userLayout->addWidget(m_userEdit);
    userLayout->setContentsMargins(0, 0, 0, 0);

    // 密码输入区
    QWidget *pwdWidget = new QWidget(mainContainer);
    pwdWidget->setStyleSheet("background-color: transparent;");
    QHBoxLayout *pwdLayout = new QHBoxLayout(pwdWidget);

    QLabel *pwdLabel = new QLabel("密码:", pwdWidget);
    pwdLabel->setStyleSheet("color: #bbbbbb; font-size: 18px; min-width: 60px;");

    m_pwdEdit = new QLineEdit(pwdWidget);
    m_pwdEdit->setPlaceholderText("请输入密码");
    m_pwdEdit->setEchoMode(QLineEdit::Password);
    m_pwdEdit->setStyleSheet(
        "background-color: #2d2d2d; color: white; padding: 10px; "
        "border-radius: 6px; border: 1px solid #3d3d3d; "
        "font-size: 16px; margin: 5px 0px;"
    );
    m_pwdEdit->setMinimumHeight(40);

    pwdLayout->addWidget(pwdLabel);
    pwdLayout->addWidget(m_pwdEdit);
    pwdLayout->setContentsMargins(0, 0, 0, 0);

    // 确认密码输入区（仅注册时显示）
    QWidget *confirmPwdWidget = new QWidget(mainContainer);
    confirmPwdWidget->setStyleSheet("background-color: transparent;");
    QHBoxLayout *confirmPwdLayout = new QHBoxLayout(confirmPwdWidget);

    QLabel *confirmPwdLabel = new QLabel("确认密码:", confirmPwdWidget);
    confirmPwdLabel->setStyleSheet("color: #bbbbbb; font-size: 18px; min-width: 80px;");

    m_confirmPwdEdit = new QLineEdit(confirmPwdWidget);
    m_confirmPwdEdit->setPlaceholderText("请再次输入密码");
    m_confirmPwdEdit->setEchoMode(QLineEdit::Password);
    m_confirmPwdEdit->setStyleSheet(
        "background-color: #2d2d2d; color: white; padding: 10px; "
        "border-radius: 6px; border: 1px solid #3d3d3d; "
        "font-size: 16px; margin: 5px 0px;"
    );
    m_confirmPwdEdit->setMinimumHeight(40);
    m_confirmPwdEdit->hide();  // 默认隐藏

    confirmPwdLayout->addWidget(confirmPwdLabel);
    confirmPwdLayout->addWidget(m_confirmPwdEdit);
    confirmPwdLayout->setContentsMargins(0, 0, 0, 0);

    // 按钮区（登录和注册按钮）
    QWidget *btnWidget = new QWidget(mainContainer);
    btnWidget->setStyleSheet("background-color: transparent;");
    QHBoxLayout *btnLayout = new QHBoxLayout(btnWidget);

    // 登录按钮
    m_loginBtn = new QPushButton("登录", btnWidget);
    m_loginBtn->setStyleSheet(
        "QPushButton { background-color: #4CAF50; color: white; "
        "font-size: 18px; border-radius: 6px; border: none; flex: 1; }"
        "QPushButton:hover { background-color: #45a049; transform: scale(1.02); }"
        "QPushButton:pressed { background-color: #3d8b40; transform: scale(0.98); }"
    );
    m_loginBtn->setMinimumHeight(45);
    m_loginBtn->setCursor(Qt::PointingHandCursor);

    // 注册按钮
    m_regBtn = new QPushButton("注册", btnWidget);
    m_regBtn->setStyleSheet(
        "QPushButton { background-color: #2196F3; color: white; "
        "font-size: 18px; border-radius: 6px; border: none; flex: 1; margin-left: 10px; }"
        "QPushButton:hover { background-color: #0b7dda; transform: scale(1.02); }"
        "QPushButton:pressed { background-color: #0a69b7; transform: scale(0.98); }"
    );
    m_regBtn->setMinimumHeight(45);
    m_regBtn->setCursor(Qt::PointingHandCursor);

    btnLayout->addWidget(m_loginBtn);
    btnLayout->addWidget(m_regBtn);
    btnLayout->setContentsMargins(0, 0, 0, 0);

    // 主布局（组织所有控件）
    QVBoxLayout *mainLayout = new QVBoxLayout(mainContainer);
    mainLayout->addWidget(titleBar);
    mainLayout->addWidget(titleLabel);
    mainLayout->addSpacing(30);
    mainLayout->addWidget(userWidget);
    mainLayout->addWidget(pwdWidget);
    mainLayout->addWidget(confirmPwdWidget);
    mainLayout->addSpacing(30);
    mainLayout->addWidget(btnWidget);
    mainLayout->setContentsMargins(40, 0, 40, 40);

    // 外层布局
    QVBoxLayout *outerLayout = new QVBoxLayout(this);
    outerLayout->addWidget(mainContainer);
    outerLayout->setContentsMargins(10, 10, 10, 10);
    mainContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    // 回车事件关联
    connect(m_pwdEdit, &QLineEdit::returnPressed, [this]() {
        if (m_confirmPwdEdit->isVisible()) {
            emit m_regBtn->clicked();
        } else {
            emit m_loginBtn->clicked();
        }
    });
    connect(m_confirmPwdEdit, &QLineEdit::returnPressed, m_regBtn, &QPushButton::click);
}

QString LoginUI::getUsername() const
{
    return m_userEdit->text().trimmed();
}

QString LoginUI::getPassword() const
{
    return m_pwdEdit->text().trimmed();
}

QString LoginUI::getConfirmPassword() const
{
    return m_confirmPwdEdit->text().trimmed();
}

void LoginUI::setLoginMode(bool isLoginMode)
{
    if (isLoginMode) {
        m_confirmPwdEdit->hide();
        m_loginBtn->setText("登录");
        m_regBtn->setText("注册");
        parentWidget()->setWindowTitle("登录");
    } else {
        m_confirmPwdEdit->show();
        m_loginBtn->setText("取消");
        m_regBtn->setText("提交注册");
        parentWidget()->setWindowTitle("注册");
    }
    clearInputs();
}

void LoginUI::setButtonsEnabled(bool enabled)
{
    m_loginBtn->setEnabled(enabled);
    m_regBtn->setEnabled(enabled);
}

void LoginUI::setLoginButtonText(const QString &text)
{
    m_loginBtn->setText(text);
}

void LoginUI::setRegisterButtonText(const QString &text)
{
    m_regBtn->setText(text);
}

void LoginUI::clearInputs()
{
    m_userEdit->clear();
    m_pwdEdit->clear();
    m_confirmPwdEdit->clear();
    m_userEdit->setFocus();
}

void LoginUI::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_isDragging = true;
        m_dragPosition = event->globalPos() - parentWidget()->frameGeometry().topLeft();
        event->accept();
    }
}

void LoginUI::mouseMoveEvent(QMouseEvent *event)
{
    if (m_isDragging && (event->buttons() & Qt::LeftButton)) {
        parentWidget()->move(event->globalPos() - m_dragPosition);
        event->accept();
    }
}

void LoginUI::mouseReleaseEvent(QMouseEvent *event)
{
    m_isDragging = false;
}
